package com.xyu.controller;
import com.xyu.po.FormData1;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:5173")
public class Controller {

    @PostMapping("/register")
    public ResponseEntity<?> handleRegistration(@RequestBody FormData1 formData) {
        try {
            // 密码长度验证
            if (formData.getPassword().length() < 6) {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("status", "error");
                errorResponse.put("message", "密码长度至少6位");
                return ResponseEntity.badRequest().body(errorResponse);
            }

            // 打印注册信息（模拟持久化）
            System.out.println("新用户注册: " + formData);

            // 构造成功响应
            Map<String, Object> successData = new HashMap<>();
            successData.put("username", formData.getUsername());
            successData.put("email", formData.getEmail());

            Map<String, Object> successResponse = new HashMap<>();
            successResponse.put("status", "success");
            successResponse.put("message", "用户注册成功");
            successResponse.put("data", successData);

            return ResponseEntity.ok(successResponse);

        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("status", "error");
            errorResponse.put("message", "服务器处理请求时发生错误");
            return ResponseEntity.internalServerError().body(errorResponse);
        }
    }
}